import { Component, Label, Node, Toggle, ToggleContainer, Tween, tween } from "cc";
import { EventManager } from "./EventManager";


export default class ComponentBase extends Component {

    //是否已经初始化节点
    public isInit: boolean = false;


    onLoad() {
        this.node.getComponents(Label).forEach((txt: Label) => {
            if (txt.useSystemFont) {
                txt.fontFamily = "Microsoft YaHei"
            }
            txt.cacheMode = Label.CacheMode.BITMAP;
        });
        this.node.children.forEach(childNode => {
            childNode.getComponents(Label).forEach((txt: Label) => {
                if (txt.useSystemFont) {
                    txt.fontFamily = "Microsoft YaHei"
                }
                txt.cacheMode = Label.CacheMode.BITMAP;
            });
        });
    }

    start() {

    }

    onEnable() {

    }

    onDisable() {
        this._dispose();
    }


    /**获得子节点 */
    public findChild(parent: Node, path: string): Node {
        let paths: Array<string> = path.split("/");
        let child: Node = parent;
        if (paths) {
            for (let i = 0; i < paths.length; ++i) {
                child = parent.getChildByName(paths[i]);
                parent = child;
                if (!parent) {
                    return;
                }
            }
        }
        return child;
    }
    /** 获取子节点 或者 子节点组件 */
    public getChild(parent: Node, childName: string, childType: any = Node): any {
        if (!parent) {
            // Logger.error(`getChild ----------- ${childName}  parent is null`);
            return;
        }
        if (childType == Node) {
            return this.findChild(parent, childName);
        }
        let childNode: Node = this.findChild(parent, childName)
        if (childNode) {
            return childNode.getComponent(childType);
        }
    }


    public hideAllChildren(node: Node) {
        for (let i = 0; i < node.children.length; i++) {
            node.children[i].active = false;
        }
    }

    public getToggleContainerIndex(toggleContainer: ToggleContainer) {
        for (let i = 0; i < toggleContainer.toggleItems.length; i++) {
            let toggle: Toggle = toggleContainer.toggleItems[i]
            if (toggle.isChecked) {
                return i;
            }
        }
        return 0
    }

    public setToggleContainerIndex(toggleContainer: ToggleContainer, index: number) {
        toggleContainer.toggleItems[toggleContainer.toggleItems.length - 1].isChecked = true
        for (let i = 0; i < toggleContainer.toggleItems.length; i++) {
            let toggle: Toggle = toggleContainer.toggleItems[i]
            if (i == index) {
                toggle.isChecked = true;
            }
        }
    }


    private _dispose() {
        // Tween.stopAll()
        // Tween.stopAllByTag(0);
        Tween.stopAllByTarget(this.node); 
        this.unscheduleAllCallbacks();
        // Logger.log("_dispose==", this.name)
        EventManager.instance.clear(this);
    }

}
